package fileProcessors.implementations;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import math.Statistics;
import fileProcessors.FileFormatException;
import fileProcessors.FileProcessor;

public class KorrelationMittelung extends FileProcessor {

	protected List<List<Double>> data;
	protected BufferedWriter outputWriter;

	public KorrelationMittelung() {
		super("Mittelung", "_result.txt", "");
	}

	@Override
	public void processFiles(File files[], File currentDirectory, File outputFile) {
		readData(files, outputFile);
		printResult(files);
	}

	protected void printResult(File files[]) {
		try {
			printStatistics(files);
			printData();
			outputWriter.close();
		} catch (IOException e) {

		}
	}

	protected void printStatistics(File[] files) throws IOException {
		for (int i = 0; i < data.size(); i++) {
			outputWriter.newLine();
			outputWriter.write("a" + i);
			outputWriter.newLine();
			outputWriter.write("Mittel:\t" + gerFormat(Statistics.mean(data.get(i))));
			outputWriter.newLine();
			outputWriter.write("Stdabw:\t" + gerFormat(Statistics.stddev(data.get(i))));
			outputWriter.newLine();
			outputWriter.write("Min:\t"
					+ gerFormat(Statistics.min(data.get(i)))
					+ "\t\tin "
					+ files[Statistics.minID(data.get(i))].getName().substring(
							files[Statistics.minID(data.get(i))].getName().length() - 13));
			outputWriter.newLine();
			outputWriter.write("Max:\t"
					+ gerFormat(Statistics.max(data.get(i)))
					+ "\t\tin "
					+ files[Statistics.maxID(data.get(i))].getName().substring(
							files[Statistics.maxID(data.get(i))].getName().length() - 13));
			outputWriter.newLine();
		}
	}

	private void printData() throws IOException {
		// //untereinander
		// for (int i = 0; i < data.length; i++) {
		// outputWriter.newLine();
		// outputWriter.write("a" + i);
		// outputWriter.newLine();
		// for (int j = 0; j < data.get(i).size(); j++) {
		// outputWriter.write("" + data.get(i).get(j));
		// outputWriter.newLine();
		// }
		// }
		// nebeneinander
		outputWriter.newLine();
		outputWriter.newLine();
		outputWriter.write("a0\ta1\ta2\ta3\ta4\ta5");
		for (int i = 0; i < data.get(0).size(); i++) {
			outputWriter.newLine();
			for (int j = 0; j < data.size(); j++) {
				outputWriter.write(gerFormat(data.get(j).get(i)) + "\t");
			}
		}
	}

	protected void readData(File[] files, File outputFile) {
		boolean errorsOccurred=false;
		// gespeicherten Datensatz l�schen
		data = new ArrayList<List<Double>>();
		for (int i = 0; i < 6; i++) {
			data.add(new ArrayList<Double>());
		}

		try {
			outputWriter = new BufferedWriter(new FileWriter(outputFile));
		} catch (IOException e2) {
		}
		for (int i = 0; i < files.length; i++) {
			BufferedReader br;
			try {
				br = new BufferedReader(new FileReader(files[i]));
				outputWriter.write("Lese "+ files[i].getName());
				outputWriter.newLine();
				String[] line = null;
				for (int j = 0; j < data.size(); j++) {
					line = br.readLine().split("\t", 100);
					// wenn hier nicht a...steht, ist die Datei ung�ltig
					if (!line[0].equals("a" + j)) {
						outputWriter.write("Parameter a" + j + " konnte nicht gelesen werden.");
						outputWriter.newLine();
						errorsOccurred=true;
					} else {
						data.get(j).add(Double.parseDouble(line[1]));
					}
				}
				br.close();
			} catch (RuntimeException e) {
				try {
					errorsOccurred=true;
					outputWriter.write("WARNUNG: Datei " + files[i].getAbsolutePath() + " konnte nicht gelesen werden.");
					outputWriter.newLine();
				} catch (IOException e1) {
					e.printStackTrace();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(errorsOccurred){
			try {
				outputWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			throw new FileFormatException();
		}
	}

	private String gerFormat(double number) {
		String ret = Double.toString(number);
		ret = ret.replace('.', ',');
		return ret;
	}

	@Override
	public String getInformation() {
		return 	"Mittelt �ber _result.txt Dateien (ausgegeben vom Maple Fit-Programm vom 05.06.2012) und " +
				"gibt von allen ausgew�hlten Dateien von a0 bis a5 Mittelwert und Standardabweichung aus. "
						+ "Au�erdem wird der eingelesene Datensatz f�r Excel in einer Tabelle formatiert dargestellt " +
						"(Dezimaltrennzeichen: Komma). Die Werte sind in der angegebenen Reihenfolge sortiert.";
	}

	@Override
	public String getDate() {
		return "23.08.2012";
	}
}
